﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine;
using Engine.Graphics;
using Game;
using MFTrain;

namespace Game
{
    public class MFWaterWidget : CanvasWidget
    {
        public bool m_textureWrap;

        public bool m_textureLinearFilter;

        public bool m_depthWriteEnabled;

        private float value;

        public MFScreenSpaceSteamRenderer steamRenderer = new MFScreenSpaceSteamRenderer(100);

        public Texture2D Texture
        {
            get;
            protected set;
        }

        public Color Color
        {
            get;
            set;
        }

        public float Value
        {
            get => value;
            set => this.value = MathUtils.Clamp(value, 0f, 1f);
        }

        public float WaterScale
        {
            get;
            set;
        }

        public float ParticlesPerSecond
        {
            get => steamRenderer.ParticlesPerSecond;
            set => steamRenderer.ParticlesPerSecond = value;
        }

        public MFWaterWidget()
        {
            Size = new Vector2(float.PositiveInfinity);
            ClampToBounds = true;
            Color = Color.Black;
            Texture = ContentManager.Get<Texture2D>("MF/Train/Water");
        }

        public override void Draw(DrawContext dc)
        {
            steamRenderer.Draw(dc.PrimitivesRenderer2D, 0f, GlobalTransform, GlobalColorTransform);

            Matrix m = GlobalTransform;
            Vector2 v = Vector2.Zero;
            var v2 = new Vector2(ActualSize.X, 0);
            Vector2 v3 = ActualSize;
            var v4 = new Vector2(0f, ActualSize.Y);
            v += new Vector2(0, ((1 - value) / 1) * Size.Y);
            v2 += new Vector2(0, ((1 - value) / 1) * Size.Y);
            Vector2.Transform(ref v, ref m, out Vector2 result);
            Vector2.Transform(ref v2, ref m, out Vector2 result2);
            Vector2.Transform(ref v3, ref m, out Vector2 result3);
            Vector2.Transform(ref v4, ref m, out Vector2 result4);
            Color color = Color * GlobalColorTransform;
            if (color.A != 0)
            {
                TexturedBatch2D texturedBatch2D = dc.PrimitivesRenderer2D.TexturedBatch(Texture, useAlphaTest: true, 1, DepthStencilState.None, null, null, SamplerState.PointWrap);
                Vector2 waterSlotsPos = Vector2.Zero;//材质水格子的起始点
                float waterSlotsWidth = (1f / 3f) * WaterScale;//材质水格子的宽度
                float hDw = ActualSize.Y * value / ActualSize.X;

                Vector2 waterPos0 = waterSlotsPos;
                Vector2 waterPos1 = waterSlotsPos + Vector2.UnitX * waterSlotsWidth;
                Vector2 waterPos2 = waterSlotsPos + Vector2.One * new Vector2(waterSlotsWidth, waterSlotsWidth * hDw);
                Vector2 waterPos3 = waterSlotsPos + Vector2.UnitY * waterSlotsWidth * hDw;
                texturedBatch2D.QueueQuad(result, result2, result3, result4, 0, waterPos0, waterPos1, waterPos2, waterPos3, color);
            }
        }

        public override void Update()
        {
            float dt = MathUtils.Clamp(Time.FrameDuration, 0f, 0.1f);
            steamRenderer.Origin = new Vector2(0f, ((1 - value) / 1) * Size.Y);
            steamRenderer.CutoffPosition = float.NegativeInfinity;
            steamRenderer.ParticleSize = 32f;
            steamRenderer.ParticleSpeed = 32f;
            steamRenderer.Width = ActualSize.X;
            steamRenderer.MinTimeToLive = 0.5f;
            steamRenderer.MaxTimeToLive = 2f;
            steamRenderer.ParticleAnimationPeriod = 1.25f;
            steamRenderer.Update(dt);
        }

        public override void MeasureOverride(Vector2 parentAvailableSize)
        {
            IsDrawRequired = true;
            DesiredSize = Size;
        }
    }
}
